<?php

namespace App\Http\Middleware\Auth;

use Closure;
use Illuminate\Support\Facades\Cookie;
use App\Services\Auth\UserConfigService;

/**
 * 应用用户配置
 */
class UserConfig
{

    /**
     * 前置 | 后置
     *
     * @param \Illuminate\Http\Request $request
     * @param \Closure $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        // 设置个人语言
        if (is_null($lang = $request->user()->config->language->lang ?? null)) {
            UserConfigService::make()->setLanguage($request->user(), $lang = config('app.locale'));
        }
        // 前端 cookie
        Cookie::queue('lang', $lang, now()->diffInMinutes(now()->addYear()), null, null, null, false);

        return $next($request);
    }
}
